<?php
/**
 * @file			account.mod.php
 * @Author			nic <nic@qq.com>
 * @Create Date:	2011-04-04
 * @Brief			帐号相关操作
 */

class account_mod {

	function account_mod()
	{
	}
    
	//初始化帐号状态
	function initAccount()
	{
		$isInit = USER::get('initAccount');
		if ($isInit) {
			return true;
		}
        $uData = APP::N('userClient')->getUserInfo();

		if(is_array($uData) && !empty($uData))
		{
			USER::uid($uData['uid']);
			//USER::set($uData);
		}
		else
		{
			USER::uid(0);
		}

		USER::set('initAccount' , '1');
	}
	
	/// 显示登录 页面
	function login()
	{
		if (USER::isUserLogin()){
			APP::redirect(URL('index'), 3);exit;
		} 
		TPL::display('login');
	}
	
	function register()
	{
		if (USER::isUserLogin()){
			APP::redirect(URL('index'), 3);exit;
		} 
		TPL::display('register');
	}	
	
	function doRegister()
	{
		$email = trim(V('p:email'));
		$mobile = trim(V('p:mobile'));
		$password = trim(V('p:password'));
		$username = trim(V('p:username'));

		if(empty($email) || empty($mobile) || empty($password) || empty($username))
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'用户名，电子邮箱，密码不能为空'))));
		}
		$insertArr = array(
			'email'=>$email,
			'mobile'=>$mobile,
			'password'=>md5($password),
			'name'=>$username,
			'regtime'=> date('Y-m-d H:i:s')
		);
		$result = DR('userCom.insertUser','',$insertArr,'');
		if((int)$result['rst'])
		{
			exit(json_encode(array('result'=>1,'data'=>array('url'=>'a'))));
		}
		else
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'注册失败，请重试'))));
		}
	}
	
	/// 登录
	function doLogin()
	{
		$username = trim(V('p:username'));
		$password = trim(V('p:password'));
		$eUrl = V('g:redirectUrl');
		$uInfo = DS('userCom.getByName','',$username);
		if(count($uInfo)==0)
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'不存在该用户'))));
		}
		$uInfo = $uInfo[0];
		if($uInfo['password'] == md5($password)) 
		{
			$uData = array('uid'=>$uInfo['id']);
			USER::uid($uInfo['id']);
			//USER::set($uData);
			APP::N('userClient')->login($uData);
			$exitUrl = $eUrl ? $eUrl : URL('index');
			exit(json_encode(array('result'=>1,'data'=>array('url'=>$exitUrl))));
		}
		else
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'密码错误'))));
		}
	}

	/// ajax登录
	function ajaxLogin()
	{
		$username = trim(V('p:username'));
		$password = trim(V('p:password'));
		$eUrl = V('g:redirectUrl');
		$uInfo = DS('userCom.getByName','',$username);
		if(count($uInfo)==0)
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'不存在该用户'))));
		}
		$uInfo = $uInfo[0];
		if($uInfo['password'] == md5($password)) 
		{
			$uData = array('uid'=>$uInfo['id']);
			USER::uid($uInfo['id']);
			//USER::set($uData);
			APP::N('userClient')->login($uData);
			$exitUrl = $eUrl ? $eUrl : URL('index');
			exit(json_encode(array('result'=>1,'data'=>array('url'=>''))));
		}
		else
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'密码错误'))));
		}
	}


	/// 退出登录
	function logout($eUrl=false)
	{
		/// 清空SESSION 
		USER::uid(0);
		USER::resetInfo();
		//清空cookie
		APP::N('userClient')->logout();
		/// 退出地址
		$exitUrl = $eUrl ? $eUrl : URL('index');
		APP::redirect($exitUrl,4);
	}
		
	/// 检查状态状态，全局使用, 是一个 preDoAction
	function gloCheckLogin()
	{
		$uid = USER::uid();
		// 未登录
		if (!$uid){
			$this->_goLogin();exit;
		}else{
			return true;
		}
	}
	
	function ucenter()
	{
		if (!USER::isUserLogin()){
			APP::redirect(URL('account.login'), 3);exit;
		} 
		$currentAction = 'myorder';
		TPL :: assign('currentAction', $currentAction);
		TPL::display('ucenter');	
	}
	
	//帐号设置
	function setting()
	{
		if (!USER::isUserLogin()){
			APP::redirect(URL('account.login'), 3);exit;
		}
		$currentAction = 'setting';
		TPL :: assign('currentAction', $currentAction);
		TPL::display('setting');			
	}
	
	//地址管理
	function address()
	{
		if (!USER::isUserLogin()){
			APP::redirect(URL('account.login'), 3);exit;
		}
		$currentAction = 'address';
		TPL::assign('currentAction', $currentAction);
		TPL::display('address');			
	}
	
	/// 默认动作
	function default_action()
	{
		$this->_goLogin();
	}
	/// 自动跳转 
	function _goLogin()
	{
		$goUrl = URL('account.login');
		APP::redirect($goUrl, 4);
	}
	
	/// 输出一段JS，通知程序关闭　登录绑定窗口，并跳转到指定页面,或者直接跳转; $goUrl 为 false　则刷新当前页
	function _onlogin($goUrl=false)
	{
			
		if (V('g:popup', false)) {
			$loadUrl = $goUrl ? '"'.addslashes($goUrl).'"' : 'false' ;
			echo '<script>try{window.opener.loginCallback('.$loadUrl.');}catch(e){window.location.href="'. W_BASE_URL . '"}</script>';
		} else {
			$loadUrl = $goUrl ? $goUrl : URL('index') ;
			APP::redirect($loadUrl,3);
		}
		exit;
	}
	
	/// 设置会话信息
	function _setLoginSession($token, $user = null)
	{
		USER::setOAuthKey($token, true);
		DS('xweibo/xwb.setToken');
		$uInfo = DR('xweibo/xwb.verifyCredentials');
		/// 用户取消之前的授权
		if ($uInfo['errno'] == '1040008') {
			/// 解除之前的绑定，重新bind
			$inData = array('access_token'=>'', 'token_secret'=>'', 'uid'=>0);
			DR('mgr/userCom.insertUser', '', $inData, $user['uid']);
			return;
		}	
		$uInfo = $uInfo['rst'];
	   //print_r($uInfo);exit;
		USER::uid($uInfo['id']);
		USER::set('uid',		$uInfo['id']);
		USER::set('screen_name',	$uInfo['screen_name']);
		USER::set('description',	$uInfo['description']);
		
		//检查当前帐号是否是　管理员 
		if ($this->_chkIsAdminAcc($uInfo['id'])){
			USER::set('isAdminAccount',	$uInfo['id']);
		}
		
		//封禁检查
		$this->_chkIsForbidden($uInfo['id']);
		return $uInfo;
	}
	
	/// 检查是否是管理员
	function _chkIsAdmin($uid)
	{
		$adm = $rs = DS('mgr/adminCom.getAdminByUid','', $uid);
		return (!empty($adm));
	}
	
	/// 检查　用户　是否被封禁
	function _chkIsForbidden($uid)
	{
		$isLoginForbidden = false;
		$uInfo = DS('mgr/userCom.getUseBanById','',$uid);
		//　在封禁表中找到记录,此用户被封禁
		if (!empty($uInfo) && is_array($uInfo) && isset($uInfo['uid']) ){
			$isLoginForbidden = $uInfo['uid'];
			USER::set('isLoginForbidden', $isLoginForbidden);
		}
		
		if ($isLoginForbidden){
			$this->_resetClientSess();
			$this->_onlogin(URL('account.inhibit'));		   
		}
	}
	
	/// 重置前端用户相关的SESSION
	function _resetClientSess()
	{
		USER::setOAuthKey(array(),	false);
		USER::setOAuthKey(array(),	true);
		USER::uid('');
		USER::set('uid',		'');
		USER::set('screen_name',	'');
		USER::set('description',	'');
	}
	
	///　封禁页面
	function inhibit()
	{
		
		TPL::display('inhibit');
		exit;
	}
	
	/// 禁止访问
	function deny()
	{
		APP::deny();
	} 

}
